package com.tinyideatech.admin.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tinyideatech.common.core.controller.BaseController;
import com.tinyideatech.common.core.entity.ApiResult;
import com.tinyideatech.common.core.entity.PageResult;
import com.tinyideatech.common.core.text.Convert;
import com.tinyideatech.common.utils.StringUtils;
import com.tinyideatech.system.entity.SysMenu;
import com.tinyideatech.system.entity.SysRole;
import com.tinyideatech.system.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/system/menu")
public class SysMenuController extends BaseController {

    @Autowired
    private ISysMenuService menuService;

    @GetMapping()
    public String menuList() {
        return "system/menu/list";
    }

    @GetMapping("/add")
    public String menuAdd() {
        return "system/menu/add";
    }

    @GetMapping("/edit/{id}")
    public String menuEdit(
            @PathVariable("id") Long id,
            ModelMap map
    ) {
        SysMenu menu = menuService.getById(id);
        map.addAttribute("menu", menu);
        return "system/menu/edit";
    }

    @ResponseBody
    @PostMapping("/list")
    public ApiResult list(SysMenu menu) {
        List<SysMenu> menuList = menuService.list();
        return success(menuList);
    }

    @ResponseBody
    @PostMapping("/tree/list")
    public ApiResult treeList() {
        List<SysMenu> menuList = menuService.list();
        return success(menuList);
    }

    @ResponseBody
    @PostMapping("/save")
    public ApiResult save(
            SysMenu sysMenu
    ) {
        menuService.saveOrUpdate(sysMenu);
        return success();
    }

    @ResponseBody
    @PostMapping("/remove")
    public ApiResult remove(
            String ids
    ){
        Long[] menuIds = Convert.toLongArray(ids);
        menuService.removeByIds(Arrays.asList(menuIds));
        return success();
    }
}
